/****************************************************************************
	This file is part of Basqet.

	Basqet is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Basqet is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
    along with Basqet.  If not, see <http://www.gnu.org/licenses/>.

****************************************************************************/

#include "richtextbroker.h"

#include "richtextedittoolbar.h"
#include "ui_mainwindow.h"

#include <QtDebug>

RichTextBroker::RichTextBroker(Ui::MainWindow* ui, RichTextEditToolBar* toolBar)
		:pMainWindow(ui),
		pRichTextEditorToolbar(toolBar)
{
}


void RichTextBroker::Connect(QObject* object, UICapabilities capabilities)
{	
	if (pConnectedObject.data() != object)
	{
		if (pConnectedObject)
		{

			Disconnect(pConnectedObject.data(), UICapabilities());

		}

		pConnectedObject = object;

		if (capabilities.canRichText())
		{
			connect(pRichTextEditorToolbar, SIGNAL(actionBold()), object, SLOT(setBold()));
			connect(pRichTextEditorToolbar, SIGNAL(actionItalic()), object, SLOT(setItalic()));
			connect(pRichTextEditorToolbar, SIGNAL(actionUnderline()), object, SLOT(setUnderline()));
			connect(pRichTextEditorToolbar, SIGNAL(actionStrikethrough()), object, SLOT(setStrikethrough()));

			connect(pRichTextEditorToolbar, SIGNAL(actionIncSize()), object, SLOT(incFontSize()));
			connect(pRichTextEditorToolbar, SIGNAL(actionDecSize()), object, SLOT(decFontSize()));

			connect(pRichTextEditorToolbar, SIGNAL(actionAdjustLeft()), object, SLOT(setLeft()));
			connect(pRichTextEditorToolbar, SIGNAL(actionAdjustMid()), object, SLOT(setMid()));
			connect(pRichTextEditorToolbar, SIGNAL(actionAdjustRight()), object, SLOT(setRight()));
		}

	}

	if (pConnectedObject)
		ActivateToolBar();
	else
		DeactivateToolBar();


}


void RichTextBroker::Disconnect(QObject* object, UICapabilities capabilities)
{

	qDebug() << "RichTextBroker::Disconnect";

	disconnect(pRichTextEditorToolbar, SIGNAL(actionBold()), object, SLOT(setBold()));
	disconnect(pRichTextEditorToolbar, SIGNAL(actionItalic()), object, SLOT(setItalic()));
	disconnect(pRichTextEditorToolbar, SIGNAL(actionUnderline()), object, SLOT(setUnderline()));
	disconnect(pRichTextEditorToolbar, SIGNAL(actionStrikethrough()), object, SLOT(setStrikethrough()));

	disconnect(pRichTextEditorToolbar, SIGNAL(actionIncSize()), object, SLOT(incFontSize()));
	disconnect(pRichTextEditorToolbar, SIGNAL(actionDecSize()), object, SLOT(decFontSize()));

	disconnect(pRichTextEditorToolbar, SIGNAL(actionAdjustLeft()), object, SLOT(setLeft()));
	disconnect(pRichTextEditorToolbar, SIGNAL(actionAdjustMid()), object, SLOT(setMid()));
	disconnect(pRichTextEditorToolbar, SIGNAL(actionAdjustRight()), object, SLOT(setRight()));

	pConnectedObject = 0;
	DeactivateToolBar();

}

void RichTextBroker::ActivateToolBar()
{
//	pMainWindow->pDockParagraph->setEnabled(true);

	pRichTextEditorToolbar->enableActions();


}

void RichTextBroker::DeactivateToolBar()
{
//	pMainWindow->pDockParagraph->setEnabled(false);
	pRichTextEditorToolbar->disableActions();

}
